﻿using System;
using Common;

public partial class Main_Weibo_Callback : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Page.IsPostBack) return;
        var code = Request.QueryString["code"];
        if (string.IsNullOrEmpty(code)) return;

        var openid = Request.QueryString["openid"];
        var openkey = Request.QueryString["openkey"];

        //返回 code openid openkey
        var service = new QQService();
        var token = service.GetAccessToken(code);
        var accesstoken=token.access_token;
        Session["access_token"] = accesstoken;
        var theUser = service.GetMyInfo(accesstoken, openid);

            if (theUser == null)
            {
                Label1.Text = "未取到用户信息";
                return;
            }
        var u = theUser.data;
        long TBuid = long.Parse(Request.Cookies["tbuid"].Value);
        int expiresin = int.Parse(token.expires_in);
        string refreshtoken = token.refresh_token;
        string nick = u.nick;
        Util.Log(u.birth_year+"-"+u.birth_month+"-"+u.birth_day);
            DateTime birthday = DateTime.Parse(u.birth_year+"-"+u.birth_month+"-"+u.birth_day);
            int fansnum = int.Parse(u.fansnum);
            int favnum = int.Parse(u.favnum);
            string head = u.head;
            int idolnum = int.Parse(u.idolnum);
            bool isent = u.isent=="1";
            bool isrealname = u.isrealname=="1";
            bool isvip = u.isvip=="1";
            string location = u.location;
            string name = u.name;
            int sex = int.Parse(u.sex);
            int tweetnum = u.tweetnum;
            string homepage = u.homepage;
            DateTime regtime = Util.GetTime(u.regtime);

        var model = new Dal.WeiBoUser
                        {
                            TBuid = TBuid,
                            accesstoken = accesstoken,
                            expiresin = expiresin,
                            refreshtoken = refreshtoken,
                            openid = openid,
                            openkey = openkey,
                            nick = nick,
                            birthday = birthday,
                            fansnum = fansnum,
                            favnum = favnum,
                            head = head,
                            idolnum = idolnum,
                            isent = isent,
                            isrealname = isrealname,
                            isvip = isvip,
                            location = location,
                            name = name,
                            sex = sex,
                            tweetnum = tweetnum,
                            homepage = homepage,
                            regtime = regtime
                        };

            var result = 0;
            if (model.Exists(TBuid,openid))
            {
                result = model.Update();
            }
            else
            {
                result = model.Add();
                var wb = "大家都来使用微博888吧!";
                var r = service.Post(accesstoken, openid, wb);//发布微博
                //r = qqService.AddFriend(token, openid);//特别收听
            }
            if (result <= 0)
            {
                Label1.Text = "授权失败!";
            }

            Response.Redirect("/WeiBoTuiGUang/WeiBoAccount.aspx");
    }
}